// 简单的测试脚本，用于验证 JDK 数据加载和解析功能
const fs = require('fs');
const path = require('path');
const os = require('os');

// 检测当前系统信息
function getCurrentSystemInfo() {
  const platform = os.platform();
  const arch = os.arch();

  let osType = 'windows';
  if (platform === 'darwin') {
    osType = 'macOS';
  } else if (platform === 'linux') {
    osType = 'linux';
  }

  let archType = 'x86_64';
  if (arch === 'arm64' || arch === 'arm') {
    archType = 'aarch64';
  }

  return { os: osType, arch: archType };
}

// 读取 jdks.json 文件
function loadJdksData() {
  try {
    const jdksPath = path.join(__dirname, 'jdks.json');
    const data = fs.readFileSync(jdksPath, 'utf8');
    const jdksData = JSON.parse(data);
    console.log('JDK 数据加载成功');
    return jdksData;
  } catch (error) {
    console.error('加载 JDK 数据失败:', error);
    return null;
  }
}

// 获取可用的选项
function getAvailableOptions(jdksData) {
  if (!jdksData || !jdksData.jdks) return null;

  const options = {
    os: new Set(),
    arch: new Set(),
    versions: new Set(),
    vendors: new Set()
  };

  jdksData.jdks.forEach(jdk => {
    options.vendors.add(jdk.vendor);
    options.versions.add(jdk.jdk_version);

    if (jdk.packages) {
      jdk.packages.forEach(pkg => {
        options.os.add(pkg.os);
        options.arch.add(pkg.arch);
      });
    }
  });

  return {
    os: Array.from(options.os).sort(),
    arch: Array.from(options.arch).sort(),
    versions: Array.from(options.versions).sort((a, b) => parseFloat(b) - parseFloat(a)),
    vendors: Array.from(options.vendors).sort()
  };
}

// 根据选择查找匹配的包
function findMatchingPackage(jdksData, os, arch, version, vendor) {
  if (!jdksData || !jdksData.jdks) return null;

  const matchingJdk = jdksData.jdks.find(jdk =>
    jdk.vendor === vendor &&
    jdk.jdk_version === version &&
    jdk.packages &&
    jdk.packages.some(pkg => pkg.os === os && pkg.arch === arch)
  );

  if (matchingJdk) {
    return matchingJdk.packages.find(pkg => pkg.os === os && pkg.arch === arch);
  }

  return null;
}

// 测试函数
function runTests() {
  console.log('开始测试 JDK 下载器功能...\n');

  // 测试1: 系统信息检测
  console.log('测试1: 系统信息检测');
  const systemInfo = getCurrentSystemInfo();
  console.log('当前系统信息:', systemInfo);
  console.log('');

  // 测试2: JDK 数据加载
  console.log('测试2: JDK 数据加载');
  const jdksData = loadJdksData();
  if (!jdksData) {
    console.error('JDK 数据加载失败，测试终止');
    return;
  }
  console.log(`加载了 ${jdksData.jdks.length} 个 JDK 条目`);
  console.log('');

  // 测试3: 获取可用选项
  console.log('测试3: 获取可用选项');
  const options = getAvailableOptions(jdksData);
  console.log('可用操作系统:', options.os);
  console.log('可用架构:', options.arch);
  console.log('可用版本 (前5个):', options.versions.slice(0, 5));
  console.log('可用供应商:', options.vendors);
  console.log('');

  // 测试4: 查找匹配的包
  console.log('测试4: 查找匹配的包');

  // 使用实际存在的版本进行测试
  const testCases = [
    { version: '21.0.8', vendor: 'Microsoft' },
    { version: '25', vendor: 'Oracle' },
    { version: '17.0.16', vendor: 'Eclipse' }
  ];

  for (const testCase of testCases) {
    const testPackage = findMatchingPackage(jdksData, systemInfo.os, systemInfo.arch, testCase.version, testCase.vendor);
    if (testPackage) {
      console.log(`使用版本 ${testCase.version} 和供应商 ${testCase.vendor} 找到匹配的包:`);
      console.log('- 文件名:', testPackage.archive_file_name);
      console.log('- 下载地址:', testPackage.url.substring(0, 100) + '...');
      console.log('- 文件大小:', (testPackage.archive_size / 1024 / 1024).toFixed(2), 'MB');
      break;
    }
  }
  console.log('');

  console.log('所有测试完成！');
}

// 运行测试
runTests();
